package serwers;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

public class Klient// implements Runnable
{
	private String hostname;
	private int port;
	Socket socketClient;
	//private boolean end;
	BufferedReader stdIn;
	String userInput;
	static String nick = null;

	public Klient(String hostname, int port) {
		this.hostname = hostname;
		this.port = port;
		//this.end = false;
	}

	public void connect() // run()
	{
		System.out
				.println("Connecting to the server: " + hostname + ":" + port);
		try {
			socketClient = new Socket(hostname, port);
		} catch (IOException e) {
			System.out.println("Ioexception#1 from connect method " + e);
		}
		System.out.println("Connection started");
		try {
			stdIn = new BufferedReader(new InputStreamReader(
					socketClient.getInputStream()));
		} catch (IOException e) {
			System.out.println("Ioexception#2 from connect method " + e);
		}
		System.out.println("Server response:");
		try {
			while ((userInput = stdIn.readLine()) != null) {
				System.out.println(userInput);
			}
		} catch (IOException e) {
			System.out.println("Ioexception#3 from connect method " + e);
		}
	}

	public static void main(String args[]) {
		String hostn = null;
		int portn = 0;
		for (int i = 0; i <= 2; i++) {
			try {
				portn = Integer.parseInt(args[0]);
			} catch (NumberFormatException e) {
				System.out.println("Wrong port number");
				return;
			}
			try {
				hostn = args[1];
			} catch (IndexOutOfBoundsException e) {
				System.out.println("Wrong portname");
				return;
			}
			try {
				nick = args[2];
			} catch (IndexOutOfBoundsException e) {
				System.out.println("Please type your nickname :)");
			}
		}
		Klient client = new Klient(hostn, portn);
		// new Thread(client).start();
		client.connect();
	}
}
